<%# encoding: utf-8%>
$(function() {
  if ($('body').hasClass('apps') && !$('body').hasClass('show')) {
    $('#apps-menu-link').addClass('menu-current');
    appsTable_Settings = {
      'bAutoWidth':      false,
      'bLengthChange':   false,
      'bStateSave':      true,
      'oLanguage':       dataTables_lang,
      'sPaginationType': 'full_numbers',
      'aoColumnDefs':    [{'bSearchable': false, 'aTargets': [8]},
                          {'bSortable': false, 'aTargets': [8]},
                          {'sType': 'string', 'aTargets': [5, 6, 7]}
                         ],
      'fnRowCallback':   function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
        var appid = '#' + nRow.id + '-tooltip';
        $(appid).tooltip({'tip': appid + 'box'});
        return nRow;
      },
      'fnDrawCallback':  function() {
        uploadAppBits_trigger = $('.uploadAppBits').overlay({
          'closeOnClick': false,
          'mask': {color: '#404040', loadSpeed: 'normal', opacity: 0.7},
          'top': '25%',
          'onBeforeLoad': function(event) {
            $('.modalBox-flash').empty();
            var form = this.getOverlay().find("#upload-app-bits-form");
            form.attr("action", this.getTrigger().attr("href"));
          }
        });
      }
    };
    appsTable = $('#apps-table').dataTable(appsTable_Settings);
    var ultabs = $('ul.tabs');
    ultabs.tabs('div.panels > div.panel', {
      effect: 'slide',
      tabs: 'li',
      onClick: function(event, tabIndex) {
        var tab = this.getCurrentTab().find('a').attr('href');
        if (tab == '#apps') {
          $('.invoke-modalBox').html('<button class="createApplication" rel="#create_application_modalBox">' + I18n.t('apps.js.add_app') + '</button>').toggle();
          createApplication_trigger = $('.createApplication').overlay({
            'closeOnClick': false,
            'mask': {color: '#404040', loadSpeed: 'normal', opacity: 0.7},
            'top': '15%',
            'onBeforeLoad': function(event) {
              $('.modalBox-flash').empty();
            }
          });
          if (typeof memory_chosen == 'undefined') {
            memory_chosen = $('.memory-chosen').chosen({'no_results_text': I18n.t('assets.js.no_results')});
          }
          if (typeof type_chosen == 'undefined') {
            type_chosen = $('.type-chosen').chosen({'no_results_text': I18n.t('assets.js.no_results')});
          }
          if (typeof service_chosen == 'undefined') {
            service_chosen = $('.service-chosen').chosen({'no_results_text': I18n.t('assets.js.no_results')});
          }
          $('#apps-table .icon-tip').tooltip({'effect': 'fade', 'tipClass': 'help-tip'});
        }
      }
    });
    $('#new-app-form .help').tooltip({'effect': 'fade', 'tipClass': 'help-tip'});
    $('#new-app-form').validator({'effect': 'below_field_error', 'inputEvent': 'blur', 'lang': validatorTools_lang});
    $(document)
      .on('ajax:beforeSend', '#new-app-form', function(evt, xhr, settings) {
        var validator = $('#new-app-form').data('validator');
        if (!validator.checkValidity()) return false;
        request_processed = false;
        $(this).find('.modalBox-submit input[type="submit"]').attr({value: I18n.t('assets.js.ajax_processing')});
      })
      .on('ajax:success', '#new-app-form', function(evt, data, status, xhr) {
        if (request_processed == true) {
          $(this).find('input[type="text"],input[type="number"],input[type="url"]').val('');
        }
      })
      .on('ajax:error', '#new-app-form', function(evt, xhr, status, error) {
        $('#flash-content').empty();
        $('#flash-content').append('<div class="alert">' + I18n.t('assets.js.ajax_error') + '</div>');
        $('#flash').fadeIn('slow');
      })
      .on('ajax:complete', '#new-app-form', function(evt, xhr, status) {
        if (request_processed == true) {
          createApplication_trigger.eq(0).overlay().close();
        }
        $(this).find('.modalBox-submit input[type="submit"]').attr({value: I18n.t('apps.js.add_app')});
      });
    $(document)
      .on('ajax:beforeSend', '#start-app-link', function(evt, xhr, settings) {
        request_processed = false;
        $(this).find('img').attr({ src : '<%= asset_path('wait.gif') %>'}).removeClass('start');
      })
      .on('ajax:error', '#start-app-link', function(evt, xhr, status, error) {
        $('#flash-content').empty();
        $('#flash-content').append('<div class="alert">' + I18n.t('assets.js.ajax_error') + '</div>');
        $('#flash').fadeIn('slow');
      })
      .on('ajax:complete', '#start-app-link', function(evt, xhr, status) {
        if (request_processed == false) {
          $(this).find('img').attr({ src : '<%= asset_path('s.gif') %>'}).addClass('start');
        }
      });
    $(document)
      .on('ajax:beforeSend', '#stop-app-link', function(evt, xhr, settings) {
        request_processed = false;
        $(this).find('img').attr({ src : '<%= asset_path('wait.gif') %>'}).removeClass('stop');
      })
      .on('ajax:error', '#stop-app-link', function(evt, xhr, status, error) {
        $('#flash-content').empty();
        $('#flash-content').append('<div class="alert">' + I18n.t('assets.js.ajax_error') + '</div>');
        $('#flash').fadeIn('slow');
      })
      .on('ajax:complete', '#stop-app-link', function(evt, xhr, status) {
        if (request_processed == false) {
          $(this).find('img').attr({ src : '<%= asset_path('s.gif') %>'}).addClass('stop');
        }
      });
    $(document)
      .on('ajax:beforeSend', '#restart-app-link', function(evt, xhr, settings) {
        request_processed = false;
        $(this).find('img').attr({ src : '<%= asset_path('wait.gif') %>'}).removeClass('restart');
      })
      .on('ajax:error', '#restart-app-link', function(evt, xhr, status, error) {
        $('#flash-content').empty();
        $('#flash-content').append('<div class="alert">' + I18n.t('assets.js.ajax_error') + '</div>');
        $('#flash').fadeIn('slow');
      })
      .on('ajax:complete', '#restart-app-link', function(evt, xhr, status) {
        if (request_processed == false) {
          $(this).find('img').attr({ src : '<%= asset_path('s.gif') %>'}).addClass('restart');
        }
      });
    $('#upload-app-bits-form .help').tooltip({'effect': 'fade', 'tipClass': 'help-tip'});
    $('#upload-app-bits-form').validator({'effect': 'below_field_error', 'inputEvent': 'blur', 'lang': validatorTools_lang});
    $(document)
      .on('ajax:beforeSend', '#upload-app-bits-form', function(evt, xhr, settings) {
        var validator = $('#upload-app-bits-form').data('validator');
        if (!validator.checkValidity()) return false;
        request_processed = false;
        $(this).find('.modalBox-submit input[type="submit"]').attr({value: I18n.t('assets.js.ajax_processing')});
      })
      .on('ajax:success', '#upload-app-bits-form', function(evt, data, status, xhr) {
        if (request_processed == true) {
          $(this).find('input[type="url"],input[type="text"]').val('');
        }
      })
      .on('ajax:error', '#upload-app-bits-form', function(evt, xhr, status, error) {
        $('#flash-content').empty();
        $('#flash-content').append('<div class="alert">' + I18n.t('assets.js.ajax_error') + '</div>');
        $('#flash').fadeIn('slow');
      })
      .on('ajax:complete', '#upload-app-bits-form', function(evt, xhr, status) {
        if (request_processed == true) {
            uploadAppBits_trigger.each(function() { $(this).overlay().close(); });
        }
        $(this).find('.modalBox-submit input[type="submit"]').attr({value: I18n.t('apps.js.upload_bits')});
      });
    $(document)
      .on('ajax:beforeSend', '#delete-app-link', function(evt, xhr, settings) {
        $(this).find('img').attr({ src : '<%= asset_path('wait.gif') %>'}).removeClass('delete');
      })
      .on('ajax:error', '#delete-app-link', function(evt, xhr, status, error) {
        $('#flash-content').empty();
        $('#flash-content').append('<div class="alert">' + I18n.t('assets.js.ajax_error') + '</div>');
        $('#flash').fadeIn('slow');
      })
      .on('ajax:complete', '#delete-app-link', function(evt, xhr, status) {
        $(this).find('img').attr({ src : '<%= asset_path('s.gif') %>'}).addClass('delete');
      });
  }
});